package com.lengtouzai.databaseexp.view.journalcomponent;


import com.lengtouzai.databaseexp.entity.Journal;
import com.lengtouzai.databaseexp.entity.Remark;
import com.lengtouzai.databaseexp.entity.User;
import com.lengtouzai.databaseexp.event.OnJournalDeleteEvent;
import com.lengtouzai.databaseexp.event.OnRemarkAddEvent;
import com.lengtouzai.databaseexp.event.OnRemarkDeleteEvent;
import com.lengtouzai.databaseexp.view.main.MainView;
import com.lengtouzai.databaseexp.view.remarkcomponent.RemarkComponent;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.html.Article;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import io.jmix.core.DataManager;
import io.jmix.core.security.CurrentAuthentication;
import io.jmix.flowui.UiComponents;
import io.jmix.flowui.UiEventPublisher;
import io.jmix.flowui.component.textarea.JmixTextArea;
import io.jmix.flowui.component.textfield.TypedTextField;
import io.jmix.flowui.kit.component.button.JmixButton;
import io.jmix.flowui.view.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;

import java.time.LocalDateTime;
import java.util.List;

@Route(value = "journal-component", layout = MainView.class)
@ViewController("exp_JournalComponent")
@ViewDescriptor("journal-component.xml")
public class JournalComponent extends StandardView {
    @Autowired
    private DataManager dataManager;
    @ViewComponent
    private Article user;
    @ViewComponent
    private H2 title;
    @ViewComponent
    private Article content;
    private Journal journal;
    @Autowired
    private UiComponents uiComponents;
    @ViewComponent
    private Div remarkList;
    @ViewComponent
    private TypedTextField<Object> createRemark;
    @Autowired
    private CurrentAuthentication currentAuthentication;
    @ViewComponent
    private VerticalLayout journalPreview;
    @ViewComponent
    private VerticalLayout journalEdit;
    @ViewComponent
    private TypedTextField<Object> titleInput;
    @ViewComponent
    private JmixTextArea contentInput;
    @Autowired
    private UiEventPublisher uiEventPublisher;

    @Subscribe(id = "edit", subject = "clickListener")
    public void onEditClick(final ClickEvent<JmixButton> event) {
        journalPreview.setVisible(false);
        journalEdit.setVisible(true);
        titleInput.setValue(title.getText());
        contentInput.setValue(content.getText());
    }

    @Subscribe(id = "cancel", subject = "clickListener")
    public void onCancelClick(final ClickEvent<JmixButton> event) {
        journalEdit.setVisible(false);
        journalPreview.setVisible(true);
    }

    @Subscribe(id = "save", subject = "clickListener")
    public void onSaveClick(final ClickEvent<JmixButton> event) {
        journal.setTitle(titleInput.getValue());
        journal.setContent(contentInput.getValue());
        journal.setUpdatedAt(LocalDateTime.now());
        dataManager.save(journal);
        journal = dataManager.load(Journal.class).query("select e from exp_Journal e where e.id = :id")
                .parameter("id", journal.getId()).one();

        title.setText(journal.getTitle());
        content.setText(journal.getContent());

        journalEdit.setVisible(false);
        journalPreview.setVisible(true);
    }

    @Subscribe(id = "delete", subject = "clickListener")
    public void onDeleteClick(final ClickEvent<JmixButton> event) {
        List<Remark> remarks = dataManager.load(Remark.class)
                .query("select e from exp_Remark e where e.toJournal = :journal")
                .parameter("journal", journal).list();
        for (Remark remark : remarks) {
            dataManager.remove(remark);
        }
        dataManager.remove(journal);
        uiEventPublisher.publishEvent(new OnJournalDeleteEvent(this));
    }

    public void setJournal(Journal journal) {
        this.journal = journal;
        this.setId(journal.getId().toString());
        user.setText(journal.getBelongs().getUsername());
        title.setText(journal.getTitle());
        content.setText(journal.getContent());

        initRemark();
    }

    private void initRemark() {
        List<Remark> remarks = dataManager.load(Remark.class)
                .query("select e from exp_Remark e where e.toJournal = :journal order by e.createdAt")
                .parameter("journal", journal).list();

        remarkList.removeAll();
        for (Remark remark : remarks) {
            RemarkComponent remarkComponent = uiComponents.create(RemarkComponent.class);
            remarkComponent.setRemark(remark);
            remarkList.add(remarkComponent);
        }
    }

    @Subscribe(id = "create", subject = "clickListener")
    public void onCreateClick(final ClickEvent<JmixButton> event) {
        Remark remark = dataManager.create(Remark.class);
        remark.setToJournal(journal);
        remark.setContent(createRemark.getValue());
        remark.setCreatedAt(LocalDateTime.now());
        remark.setBelongs((User) currentAuthentication.getUser());
        dataManager.save(remark);


        RemarkComponent remarkComponent = uiComponents.create(RemarkComponent.class);
        remarkComponent.setRemark(remark);
        remarkList.add(remarkComponent);

        createRemark.clear();
    }

    @EventListener
    private void onRemarkAdd(OnRemarkAddEvent event) {
        initRemark();
    }

    @EventListener
    private void onRemarkDelete(OnRemarkDeleteEvent event) {
        initRemark();
    }
}